///////////////////////////////////////////////////////////////
// File: "CTrophyState.h"
// Author: Ryan Burkevich (RB)
// Purpose: To contain all trophy select functionality.
///////////////////////////////////////////////////////////////	

#include "CTrophyState.h"
#include "CProfileManager.h"
#include "CBitmapFontEngine.h"
#include "CWiimote.h"
#include "CLevelSelectState.h"
#include "CGame.h"
#include "CTransitionScreen.h"
#include "CGamePlayState.h"
#include "../Wrappers/CSGD_DirectInput.h"
#include "../Wrappers/CSGD_TextureManager.h"
#include "CSoundManager.h"


CTrophyState::CTrophyState()
{
	m_bItsTrophyTime = false;
	m_nFontID = -1;
	m_nCurrentLevel = 1;
	m_nSelectedTrophy = 0;
	m_nLRChoice = 0;
	for(int i = 0; i < 3; i++)
		m_vTrophyIDs.push_back(-1);
}

CTrophyState::~CTrophyState()
{

}

void CTrophyState::Enter()
{
	m_cScreen.LoadScreen("Screens/BS_LevelSelectScreen.xml");
	m_nFontID = CBitmapFontEngine::GetInstance()->LoadFont("BS_TechDemoTitle.xml");

	m_nLRChoice = 0;
	m_nSelectedTrophy = 0;
	m_nCurrentLevel = CProfileManager::GetInstance()->GetCurrentProfile()->GetWhichLevelPlaying();

	for(int i = 0; i < 3; i++)
		m_vTrophyIDs[i] = CSGD_TextureManager::GetInstance()->LoadTexture("Resource/Trophies/BS_GoldTrophy.png");
	m_bItsTrophyTime = false;
}

void CTrophyState::Exit()
{
	m_cScreen.ExitScreen();

	m_nLRChoice = 0;
	m_nSelectedTrophy = 0;

	for(int i = 0; i < 3; i++)
		CSGD_TextureManager::GetInstance()->ReleaseTexture(m_vTrophyIDs[i]);

	m_bItsTrophyTime = false;
}

bool CTrophyState::Input(float fElapsedTime)
{
	CWiimote* pWii = CWiimote::GetInstance();
	CSGD_DirectInput* pDI = CSGD_DirectInput::GetInstance();
	CProfileManager* pPM = CProfileManager::GetInstance();

	if (pWii->IsButtonJustPressed(WII_HOME) || pDI->KeyPressed(DIK_ESCAPE))
	{
		CSoundManager::GetInstance()->PlaySoundIndxUnBuffered(SOUND_MENU_SELECT);
		if(m_bItsTrophyTime)
		{
			m_bItsTrophyTime = false;
			m_nSelectedTrophy = 0;
		}
		else
			CGame::GetInstance()->ChangeState(CLevelSelectState::GetInstance());
	}

	if(!m_bItsTrophyTime)
	{
		if(pWii->IsButtonJustPressed(WII_LEFT) || pDI->KeyPressed(DIK_LEFT))
		{
			CSoundManager::GetInstance()->PlaySoundIndxUnBuffered(SOUND_MENU_MOVE);
			m_nLRChoice--;
		}
		else if(pWii->IsButtonJustPressed(WII_RIGHT) || pDI->KeyPressed(DIK_RIGHT))
		{
			CSoundManager::GetInstance()->PlaySoundIndxUnBuffered(SOUND_MENU_MOVE);
			m_nLRChoice++;
		}
	}

	if(m_nLRChoice > 2)
		m_nLRChoice = 0;
	else if(m_nLRChoice < 0)
		m_nLRChoice = 2;	

	if (pWii->IsButtonJustPressed(WII_A) || pDI->KeyPressed(DIK_RETURN))
	{
		CSoundManager::GetInstance()->PlaySoundIndxUnBuffered(SOUND_MENU_SELECT);
		switch(m_nLRChoice)
		{
		case 0:
			if(pPM->GetCurrentProfile()->CheckForTrophy(1, m_nCurrentLevel))
				m_bItsTrophyTime = true;
				m_nSelectedTrophy = 1;
			break;
		case 1:
			if(pPM->GetCurrentProfile()->CheckForTrophy(2, m_nCurrentLevel))
				m_bItsTrophyTime = true;
				m_nSelectedTrophy = 2;
			break;
		case 2:
			if(pPM->GetCurrentProfile()->CheckForTrophy(3, m_nCurrentLevel))
				m_bItsTrophyTime = true;
				m_nSelectedTrophy = 3;
			break;
		}
	}

	return true;
}

void CTrophyState::Update(float fElapsedTime)
{

}

void CTrophyState::Render()
{
	m_cScreen.RenderScreen();

	CBitmapFontEngine* pBFE = CBitmapFontEngine::GetInstance();
	CProfileManager* pPM = CProfileManager::GetInstance();

	int TopX= m_cScreen.GetPosObjMembers("Top",XPOS);
	int TopY= m_cScreen.GetPosObjMembers("Top",YPOS);

	int LeftX = m_cScreen.GetPosObjMembers("Left",XPOS);
	int LeftY = m_cScreen.GetPosObjMembers("Left",YPOS);

	int RightX = m_cScreen.GetPosObjMembers("Right",XPOS);
	int RightY = m_cScreen.GetPosObjMembers("Right",YPOS);

	char temp[30];

	sprintf_s(temp, "Level %i", m_nCurrentLevel);
	pBFE->RenderString("Trophies", m_nFontID, 0, D3DCOLOR_XRGB(0, 255, 0), TopX+40, TopY+25, 0);
	pBFE->RenderString(temp, m_nFontID, 0, D3DCOLOR_XRGB(0, 255, 0), LeftX, LeftY, 0);

	if(m_bItsTrophyTime && m_nSelectedTrophy == 1)
	{
		char szFastestTime[50];

		CSGD_TextureManager::GetInstance()->Draw(m_vTrophyIDs[0], RightX+60, RightY+60, .5, .5, 0, 0, 0, 0, 0xffffffff);
		pBFE->RenderString("Fastest", m_nFontID, 0, 0xffff0000, LeftX, LeftY+35, 0);
		pBFE->RenderString("Time", m_nFontID, 0, 0xffff0000, LeftX+35, LeftY+70, 0);

		pBFE->RenderString(pPM->GetFastestTimeName(m_nCurrentLevel), m_nFontID, 0, 0xffffffff, LeftX+15, LeftY+115, 0, .8f);
		sprintf_s(szFastestTime, "%.f", pPM->GetFastestTime(m_nCurrentLevel));
		pBFE->RenderString(szFastestTime, m_nFontID, 0, 0xff0000ff, LeftX+25, LeftY+170, 0);
		pBFE->RenderString("sec", m_nFontID, 0, 0xff0000ff, LeftX+125, LeftY+205, 0);
	}
	else if(m_bItsTrophyTime && m_nSelectedTrophy == 2)
	{
		CSGD_TextureManager::GetInstance()->Draw(m_vTrophyIDs[0], RightX+60, RightY+60, .5, .5, 0, 0, 0, 0, 0xffffffff);
		pBFE->RenderString("No Death", m_nFontID, 0, 0xffff0000, LeftX+15, LeftY+100, 0, .8f);

	}
	else if(m_bItsTrophyTime && m_nSelectedTrophy == 3)
	{
		if(m_nCurrentLevel == 1)
		{
			CSGD_TextureManager::GetInstance()->Draw(m_vTrophyIDs[0], RightX+60, RightY+60, .5, .5, 0, 0, 0, 0, 0xffffffff);
			pBFE->RenderString("Beat level", m_nFontID, 0, 0xffff0000, LeftX+15, LeftY+65, 0, .8f);
			pBFE->RenderString("in under", m_nFontID, 0, 0xffff0000, LeftX+25, LeftY+115, 0, .8f);
			pBFE->RenderString("10 Secs", m_nFontID, 0, 0xffff0000, LeftX+35, LeftY+155, 0, .8f);
		}
		else if(m_nCurrentLevel == 2)
		{
			CSGD_TextureManager::GetInstance()->Draw(m_vTrophyIDs[0], RightX+60, RightY+60, .5, .5, 0, 0, 0, 0, 0xffffffff);
			pBFE->RenderString("Destroyed", m_nFontID, 0, 0xffff0000, LeftX+20, LeftY+65, 0, .8f);
			pBFE->RenderString("Target", m_nFontID, 0, 0xffff0000, LeftX+45, LeftY+115, 0, .8f);
			pBFE->RenderString("Laser", m_nFontID, 0, 0xffff0000, LeftX+65, LeftY+155, 0, .8f);
		}
		else if(m_nCurrentLevel == 3)
		{
			CSGD_TextureManager::GetInstance()->Draw(m_vTrophyIDs[0], RightX+60, RightY+60, .5, .5, 0, 0, 0, 0, 0xffffffff);
			pBFE->RenderString("Under", m_nFontID, 0, 0xffff0000, LeftX+30, LeftY+65, 0, .8f);
			pBFE->RenderString("3", m_nFontID, 0, 0xffff0000, LeftX+45, LeftY+115, 0);
			pBFE->RenderString("Rotations", m_nFontID, 0, 0xffff0000, LeftX+20, LeftY+155, 0, .8f);
		}
		else if(m_nCurrentLevel == 4)
		{
			CSGD_TextureManager::GetInstance()->Draw(m_vTrophyIDs[0], RightX+60, RightY+60, .5, .5, 0, 0, 0, 0, 0xffffffff);
			pBFE->RenderString("Beat level", m_nFontID, 0, 0xffff0000, LeftX+20, LeftY+65, 0, .8f);
			pBFE->RenderString("with key", m_nFontID, 0, 0xffff0000, LeftX+30, LeftY+115, 0, .8f);
			pBFE->RenderString("left in", m_nFontID, 0, 0xffff0000, LeftX+40, LeftY+155, 0, .8f);
			pBFE->RenderString("inventory", m_nFontID, 0, 0xffff0000, LeftX+20, LeftY+195, 0, .8f);
		}
		else if(m_nCurrentLevel == 5)
		{
			CSGD_TextureManager::GetInstance()->Draw(m_vTrophyIDs[0], RightX+60, RightY+60, .5, .5, 0, 0, 0, 0, 0xffffffff);
			pBFE->RenderString("Killed a", m_nFontID, 0, 0xffff0000, LeftX+30, LeftY+65, 0, .8f);
			pBFE->RenderString("jumping", m_nFontID, 0, 0xffff0000, LeftX+25, LeftY+115, 0, .8f);
			pBFE->RenderString("Enemy", m_nFontID, 0, 0xffff0000, LeftX+50, LeftY+155, 0, .8f);
		}
		else if(m_nCurrentLevel == 6)
		{
			CSGD_TextureManager::GetInstance()->Draw(m_vTrophyIDs[0], RightX+60, RightY+60, .5, .5, 0, 0, 0, 0, 0xffffffff);
			pBFE->RenderString("Used both", m_nFontID, 0, 0xffff0000, LeftX+30, LeftY+65, 0, .8f);
			pBFE->RenderString("Keys", m_nFontID, 0, 0xffff0000, LeftX+50, LeftY+115, 0, .8f);
		}
		else if(m_nCurrentLevel == 7)
		{
			CSGD_TextureManager::GetInstance()->Draw(m_vTrophyIDs[0], RightX+60, RightY+60, .5, .5, 0, 0, 0, 0, 0xffffffff);
			pBFE->RenderString("Finished", m_nFontID, 0, 0xffff0000, LeftX+30, LeftY+65, 0, .8f);
			pBFE->RenderString("with 1", m_nFontID, 0, 0xffff0000, LeftX+25, LeftY+115, 0, .8f);
			pBFE->RenderString("of each", m_nFontID, 0, 0xffff0000, LeftX+30, LeftY+155, 0, .8f);
			pBFE->RenderString("Bomb", m_nFontID, 0, 0xffff0000, LeftX+65, LeftY+195, 0, .8f);
		}
		else if(m_nCurrentLevel == 8)
		{
			CSGD_TextureManager::GetInstance()->Draw(m_vTrophyIDs[0], RightX+60, RightY+60, .5, .5, 0, 0, 0, 0, 0xffffffff);
			pBFE->RenderString("Killed", m_nFontID, 0, 0xffff0000, LeftX+30, LeftY+65, 0, .8f);
			pBFE->RenderString("3", m_nFontID, 0, 0xffff0000, LeftX+45, LeftY+115, 0);
			pBFE->RenderString("Enemies", m_nFontID, 0, 0xffff0000, LeftX+20, LeftY+155, 0, .8f);
		}
		else if(m_nCurrentLevel == 9)
		{
			CSGD_TextureManager::GetInstance()->Draw(m_vTrophyIDs[0], RightX+60, RightY+60, .5, .5, 0, 0, 0, 0, 0xffffffff);
			pBFE->RenderString("Killed", m_nFontID, 0, 0xffff0000, LeftX+30, LeftY+65, 0, .8f);
			pBFE->RenderString("1", m_nFontID, 0, 0xffff0000, LeftX+45, LeftY+115, 0);
			pBFE->RenderString("Enemy", m_nFontID, 0, 0xffff0000, LeftX+20, LeftY+155, 0, .8f);
		}
		else if(m_nCurrentLevel == 10)
		{
			CSGD_TextureManager::GetInstance()->Draw(m_vTrophyIDs[0], RightX+60, RightY+60, .5, .5, 0, 0, 0, 0, 0xffffffff);
			pBFE->RenderString("Destroyed", m_nFontID, 0, 0xffff0000, LeftX+25, LeftY+65, 0, .8f);
			pBFE->RenderString("6 or more", m_nFontID, 0, 0xffff0000, LeftX+25, LeftY+115, 0, .8f);
			pBFE->RenderString("Blocks", m_nFontID, 0, 0xffff0000, LeftX+40, LeftY+155, 0, .8f);
		}
	}
	else
	{
		int TrophyX = LeftX+8;
		int TrophyY = LeftY+170;

		m_dwTrophy1Color = 0xff323232;
		m_dwTrophy2Color = 0xff323232;
		m_dwTrophy3Color = 0xff323232;

		for(int i = 1; i < 4; i++)
		{
			if(pPM->GetCurrentProfile()->CheckForTrophy(i, m_nCurrentLevel))
			{
				switch(i)
				{
				case 1:
					m_dwTrophy1Color = 0xffffffff;
					break;
				case 2:
					m_dwTrophy2Color = 0xffffffff;
					break;
				case 3:
					m_dwTrophy3Color = 0xffffffff;
					break;
				}
			}
		}

		if(m_nLRChoice == 0)
		{
			CSGD_TextureManager::GetInstance()->Draw(m_vTrophyIDs[0], TrophyX, TrophyY-35, .25, .25, 0, 0, 0, 0, m_dwTrophy1Color);
			CSGD_TextureManager::GetInstance()->Draw(m_vTrophyIDs[1], TrophyX+75, TrophyY, .25, .25, 0, 0, 0, 0, m_dwTrophy2Color);
			CSGD_TextureManager::GetInstance()->Draw(m_vTrophyIDs[2], TrophyX+150, TrophyY, .25, .25, 0, 0, 0, 0, m_dwTrophy3Color);
			if(pPM->GetCurrentProfile()->CheckForTrophy(1, m_nCurrentLevel))
			{
				pBFE->RenderString("Fastest", m_nFontID, 0, D3DCOLOR_XRGB(0, 255, 0), RightX, RightY+65, 0);
				pBFE->RenderString("Time", m_nFontID, 0, D3DCOLOR_XRGB(0, 255, 0), RightX+35, RightY+100, 0);
				pBFE->RenderString(pPM->GetFastestTimeName(m_nCurrentLevel), m_nFontID, 0, 0xff323232, RightX+105, RightY+135, 0, .8f);
			}
			else
			{
				pBFE->RenderString("Locked", m_nFontID, 0, 0xff323232, LeftX, LeftY+90, 0);
				pBFE->RenderString("Fastest", m_nFontID, 0, 0xff323232, RightX, RightY+65, 0);
				pBFE->RenderString("Time", m_nFontID, 0, 0xff323232, RightX+35, RightY+100, 0);
				pBFE->RenderString(pPM->GetFastestTimeName(m_nCurrentLevel), m_nFontID, 0, 0xff323232, RightX+105, RightY+135, 0, .8f);
			}
		}
		else if(m_nLRChoice == 1)
		{
			CSGD_TextureManager::GetInstance()->Draw(m_vTrophyIDs[0], TrophyX, TrophyY, .25, .25, 0, 0, 0, 0, m_dwTrophy1Color);
			CSGD_TextureManager::GetInstance()->Draw(m_vTrophyIDs[1], TrophyX+75, TrophyY-35, .25, .25, 0, 0, 0, 0, m_dwTrophy2Color);
			CSGD_TextureManager::GetInstance()->Draw(m_vTrophyIDs[2], TrophyX+150, TrophyY, .25, .25, 0, 0, 0, 0, m_dwTrophy3Color);
			if(pPM->GetCurrentProfile()->CheckForTrophy(2, m_nCurrentLevel))
				pBFE->RenderString("No Death", m_nFontID, 0, D3DCOLOR_XRGB(0, 255, 0), RightX+115, RightY+100, 0, .8f);
			else
			{
				pBFE->RenderString("Locked", m_nFontID, 0, 0xff323232, LeftX, LeftY+90, 0);
				pBFE->RenderString("No Death", m_nFontID, 0, 0xff323232, RightX+115, RightY+100, 0, .8f);
			}
		}
		else if(m_nLRChoice == 2)
		{
			CSGD_TextureManager::GetInstance()->Draw(m_vTrophyIDs[0], TrophyX, TrophyY, .25, .25, 0, 0, 0, 0, m_dwTrophy1Color);
			CSGD_TextureManager::GetInstance()->Draw(m_vTrophyIDs[1], TrophyX+75, TrophyY, .25, .25, 0, 0, 0, 0, m_dwTrophy2Color);
			CSGD_TextureManager::GetInstance()->Draw(m_vTrophyIDs[2], TrophyX+150, TrophyY-35, .25, .25, 0, 0, 0, 0, m_dwTrophy3Color);
			if(pPM->GetCurrentProfile()->CheckForTrophy(3, m_nCurrentLevel))
			{
				pBFE->RenderString("Challenge", m_nFontID, 0, D3DCOLOR_XRGB(0, 255, 0), RightX+105, RightY+85, 0, .8f);
				pBFE->RenderString("Beaten", m_nFontID, 0, D3DCOLOR_XRGB(0, 255, 0), RightX+125, RightY+120, 0, .8f);
			}
			else
			{
				pBFE->RenderString("Locked", m_nFontID, 0, 0xff323232, LeftX, LeftY+90, 0);
				pBFE->RenderString("Challenge", m_nFontID, 0, 0xff323232, RightX+105, RightY+85, 0, .8f);
				pBFE->RenderString("Beaten", m_nFontID, 0, 0xff323232, RightX+125, RightY+120, 0, .8f);
			}
		}
	}
}